﻿using BaseModel.MesDb;
using BaseTool;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BaseModel
{
    public static class DataDb
    {
        public static SqlSugarScope dbBox = new SqlSugarScope(new ConnectionConfig()
        {
            ConnectionString = $"Data Source={Path.Combine(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "data"), "datanum.db3")};Version=3;journal_mode=WAL;",
            DbType = DbType.Sqlite,//[Sqlite]安装[System.Data.SQLite];
            IsAutoCloseConnection = true
        }, db =>
        {
            db.Aop.OnError = ex =>
            {
                LogHelper.WriteError("sqlite失败", ex);
            };
        });
        public static SqlSugarScope dbMes = new SqlSugarScope(new ConnectionConfig()
        {
            ConnectionString = "Data Source=192.168.2.8;Database=EdgeDB;Uid=sa;Pwd=8ik,9ol.",
            DbType = DbType.SqlServer,//[MySqlConnector]安装[SqlSugar.MySqlConnector]+[MySqlConnector];[MySql]安装[MySql.Data];
            IsAutoCloseConnection = true
        }, db =>
        {
            db.Aop.OnError = ex =>
            {
                LogHelper.WriteError("SqlServer失败", ex);
            };
        });
        /// <summary>
        /// 初始化数据
        /// </summary>
        public static void Inidata()
        {
            //不存在创建数据库，存在不会创建
            dbBox.DbMaintenance.CreateDatabase();
            //创建表根据实体类
            dbBox.CodeFirst.InitTables(typeof(PrintInfo));
            //初始化数据
            if (!dbBox.Queryable<PrintInfo>().Any())
            {
                var dt = DateTime.Now;
                var data = new PrintInfo()
                {
                    MACtext = "测试扫描长度",
                    Type = "ng1",
                    Product = "桌牌",
                    Time = dt,
                };

                dbBox.Insertable(data).ExecuteCommand();

            }
        }
    }
}
